
$(function() {

	exten_check("#c_unconditional", "#exten_unconditional");
	exten_check("#c_unavailable", "#exten_unavailable");
	exten_check("#c_busy", "#exten_busy");	
		
	$("#c_unconditional").change(function() {		
		exten_toggle(this, "#exten_unconditional");
	});
	
	$("#c_unavailable").change(function() {		
		exten_toggle(this, "#exten_unavailable");
	});
	
	$("#c_busy").change(function() {
		exten_toggle(this, "#exten_busy");
	});
	
	$(".on_off :checkbox").iphoneStyle({checkedLabel: '开', uncheckedLabel: '关'});	
});

function exten_toggle(c, exten) {
	if ($(c).attr("checked")) {
		$(exten).attr("disabled", false);
	} else {
		$(exten).attr("disabled", true);
	}
}

function exten_check(c, exten) {
	var cf_ns = $(exten).val();
	if (cf_ns == null || cf_ns == '') {
		$(exten).attr("disabled", true);
	} else {
		$(exten).attr("disabled", false);
		$(c).attr("checked", true);
	}
}

function exten(form) {	
	var body = { };
	
	var cf = '';
	var cfu = '';
	var cfb = '';
	var dnd = 'NO';
	
	// var regex = /^(2|6|8)(\d{3,5})$/;
	var regex = /^(1906|1966006|2|6|8)(\d{3,5})$/;
	
	if ($("#c_unconditional").attr("checked")) {
		cf = form.exten_unconditional.value;
		
		if (!regex.test(cf)) {
			alert("分机设置有误，请重新设置！");
			return true;
		}
	}
	
	if ($("#c_unavailable").attr("checked")) {
		cfu = form.exten_unavailable.value;
		
		if (!regex.test(cfu)) {
			alert("分机设置有误，请重新设置！");
			return true;
		}
	}
	
	if ($("#c_busy").attr("checked")) {
		cfb = form.exten_busy.value;
		
		if (!regex.test(cfb)) {
			alert("分机设置有误，请重新设置！");
			return true;
		}
	}
	
	if ($("#c_dnd").attr("checked")) {
		dnd = 'YES';
	}

	body.cf = cf;
	body.cfu = cfu;
	body.cfb = cfb;
	body.dnd = dnd;
	
	// alert($.toJSON(body));
	
	var url = 'tel.php?do=CdrMy.settingPost';
	
	$.post(url, body, function(data) {
		alert("设置成功！");
	});	
} 
